using Curriculum.Modelo.Clases;
using Utiles;

namespace Concurso.WebUserControls.RolesGenericos
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
	using Curriculum.Services;
	using System.Collections;

	/// <summary>
	///		Summary description for ABMRolGenerico.
	/// </summary>
	public class ABMRolGenerico : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.TextBox txtApellido;
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.Button btnBuscar;
		protected System.Web.UI.WebControls.Button btnLimpiar;
		protected System.Web.UI.WebControls.DataGrid dtgRoles;
		protected System.Web.UI.WebControls.Label lblCount;
		protected System.Web.UI.WebControls.Button btnNuevo;
	
		private const string pidControlAltaRolGenerico="020";

		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!this.IsPostBack)
			{				
				InitializeVariables();				
			}
		}

		#region Properties
		public string FiltroNombre
		{
			get{ return (string)ViewState["_filtroNombre"];}
			set{ ViewState["_filtroNombre"] = value;}
		}

		public string FiltroApellido
		{
			get{ return (string)ViewState["_filtroApellido"];}
			set{ ViewState["_filtroApellido"] = value;}
		}

		public string SortColumn
		{
			get{ return (string)ViewState["SortColumn"];}
			set{ ViewState["SortColumn"] = value;}
		}
		public bool SortDirection
		{
			get{ return (bool)ViewState["SortDirection"];}
			set{ ViewState["SortDirection"] = value;}
		}
		#endregion Properties

		private void InitializeVariables()
		{
			SortColumn = "Apellido";
			SortDirection = true;			
		}

		private void FillSearchVariables() 
		{
			FiltroApellido = txtApellido.Text.Trim() != string.Empty ? txtApellido.Text.Trim() : string.Empty;
			FiltroNombre = txtNombre.Text.Trim() != string.Empty ? txtNombre.Text.Trim() : string.Empty;			
		}

		private IList GetListBySearch() 
		{
			RolGenericoService service = new RolGenericoService();
			return service.GetRolGenerico(FiltroApellido,FiltroNombre);			
		}

		private void FillDataGridWithSearch()
		{
			IList list = GetListBySearch();
			((ArrayList)list).Sort(new GenericComparer(SortColumn,SortDirection));
			dtgRoles.DataSource = list;
			lblCount.Text = list.Count.ToString();
		
			try
			{
				dtgRoles.DataBind();
			}
			catch(Exception exc )
			{
				string msg = exc.Message;
				dtgRoles.CurrentPageIndex = 0;
				FillDataGridWithSearch();
			}						
		}

		private void RedirectToABMRoles(PageMode mode, string rolId)
		{
			string url = "Default.aspx?pid=" + pidControlAltaRolGenerico + "&rolId=" + rolId + "&PageMode=" + PageModeConverter.ToInt( mode );
			Server.Transfer(url);
		}

		private void EliminarRolGenerico(string rolId)
		{
			RolGenericoService servicio = new RolGenericoService();
			servicio.EliminarRolGenerico(rolId);
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnBuscar.Click += new System.EventHandler(this.btnBuscar_Click);
			this.btnLimpiar.Click += new System.EventHandler(this.btnLimpiar_Click);
			this.dtgRoles.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dtgRoles_ItemCommand);
			this.dtgRoles.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dtgRoles_PageIndexChanged);
			this.dtgRoles.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.dtgRoles_SortCommand);
			this.btnNuevo.Click += new System.EventHandler(this.btnNuevo_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnBuscar_Click(object sender, System.EventArgs e)
		{
			dtgRoles.CurrentPageIndex=0;
			FillSearchVariables();
			FillDataGridWithSearch();
		}

		private void btnLimpiar_Click(object sender, System.EventArgs e)
		{
			txtApellido.Text = string.Empty;
			txtNombre.Text = string.Empty;		
		}

		private void btnNuevo_Click(object sender, System.EventArgs e)
		{
			string url = "Default.aspx?pid=" + pidControlAltaRolGenerico;
			Server.Transfer(url);
		}

		private void dtgRoles_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
		{
			if (SortColumn.ToString() == e.SortExpression.ToString())
				SortDirection = !SortDirection;
			
			SortColumn = e.SortExpression;
			FillDataGridWithSearch();
		}

		private void dtgRoles_PageIndexChanged(object sender, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
		{
			dtgRoles.CurrentPageIndex = e.NewPageIndex;
			FillDataGridWithSearch();
		}

		private void dtgRoles_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if ( e.CommandName != "Page" ) 
			{
				string rolId = e.Item.Cells[0].Text;				
				switch ( e.CommandName ) 
				{
					case "View":
						RedirectToABMRoles(PageMode.VIEW,rolId);
						break;

					case "Modify":
						RedirectToABMRoles(PageMode.EDIT,rolId);
						break;
						
					case "Delete":
						EliminarRolGenerico(rolId);	
						FillDataGridWithSearch();
						break;
				}
			}
		}
	}
}

